<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Service\Common\OssService;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Kuafu\File\FileUtil;

class FileController extends BaseController
{
    public function index()
    {
        return view('common.file.upload');
    }

    public function upload(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|file',
        ]);
        $uploadedFile = $request->file('file');
        if (!$uploadedFile instanceof UploadedFile) {
            return $this->fail(0, '请上传文件');
        }
        $suffix = FileUtil::guessSuffix($uploadedFile->getClientOriginalName());
        return $this->success(OssService::uploadFile($uploadedFile->getRealPath(), $suffix));
    }

    /**
     * 直连OSS
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUploadAccess()
    {
        return $this->success(OssService::getUploadAccess());
    }


    /**
     * 直连OSS
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSTSAccessInfo()
    {
        return $this->success(OssService::getSTSAccessInfo());
    }
}
